<?php

namespace Kanin\Games\lib;

use Swoole\Database\RedisConfig;
use \Swoole\Database\RedisPool;

/**
 * 连接池初始化
 */
class Pool {
	/**
	 * @var RedisPool
	 */
	public static $redis;

	/**
	 * @var \Kanin\Games\lib\MongoPool
	 */
	public static $mongo;

	private function __construct() {

	}

	public static function init() {
		self::$redis = self::redisInit();
		self::$mongo = self::mongoInit();
	}

	/**
	 * redis连接池初始化
	 * @return \Swoole\Database\RedisPool
	 */
	private static function redisInit() {
		//redis初始化
		$config = Config::get('redis.user');
		$redisConfig = (new RedisConfig)
			->withHost($config['host'])
			->withPort($config['port'])
			->withAuth($config['auth'])
			->withDbIndex(0)
			->withTimeout(1);
		return new RedisPool($redisConfig);
	}

	/**
	 * mongo连接池初始化
	 * @return \Kanin\Games\lib\MongoPool
	 */
	private static function mongoInit() {
		return new MongoPool(Config::get('mongo.main'));
	}

}